{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a1a0c364-f3d6-4c73-b7ef-5d76906a92af",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 [0.0, -1.5, 0.6]\n",
      "1 [np.float64(2.4), np.float64(-1.2), np.float64(0.6)]\n",
      "2 [np.float64(1.7999999999999998), np.float64(-1.2), np.float64(0.6)]\n",
      "[np.float64(1.7999999999999998), np.float64(-1.2), np.float64(0.6)]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "def guass(A,b):\n",
    "        n=len(A)\n",
    "        for k in range(n):\n",
    "                if A[k][k]==0:\n",
    "                        print(\"pivot==0\")\n",
    "                        return\n",
    "                else:\n",
    "                        for i in range(k+1,n):\n",
    "                                factor=A[i][k]/A[k][k]\n",
    "                                for j in range(n):\n",
    "                                        A[i][j]=A[i][j]-factor*A[k][j]\n",
    "                                b[i]=b[i]-factor*b[k]\n",
    "        x=[0 for i in range(n)]\n",
    "        for i in range(n-1,-1,-1):\n",
    "                x[i]=b[i]\n",
    "                for j in range(i+1,n):\n",
    "                        x[i]=x[i]-A[i][j]*x[j]\n",
    "\n",
    "                x[i]=x[i]/A[i][i]\n",
    "\n",
    "\n",
    "        return A,b,x\n",
    "\n",
    "def gs_iter(A,b,x0,tol=1e-6,max_iter=10000):\n",
    "        n=len(b)\n",
    "        x=x0\n",
    "        for k in range(max_iter):\n",
    "                for i in range(n):\n",
    "                        s=sum(A[i][j]*x[j] for j in range(n) if j!=i)\n",
    "\n",
    "                        x[i]=(b[i]-s)/A[i][i]\n",
    "                print(k,x)\n",
    "                A=np.array(A)\n",
    "                if all(abs(A.dot(x)-b)<tol):\n",
    "                        return x\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "        A0=[[1,2,1],[2,2,3],[-1,0,3]]\n",
    "        b0=[0,3,0]\n",
    "        A,B,x=guass(A0,b0)\n",
    "        #print(A,b,x)\n",
    "        x0=[0.,0.,0.]\n",
    "        x=gs_iter(A0,b0,x0)\n",
    "        print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "9063ac88-af14-403c-b4d3-51e7090d7982",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 [0.5, 0.6666666666666666, 0.1428571428571429, 3.9999999999999982]\n",
      "1 [np.float64(-2.38095238095238), np.float64(-3.142857142857141), np.float64(0.9999999999999998), np.float64(3.9999999999999982)]\n",
      "2 [np.float64(0.14285714285714235), np.float64(-1.9999999999999984), np.float64(0.9999999999999998), np.float64(3.9999999999999982)]\n",
      "3 [np.float64(-1.0000000000000002), np.float64(-1.9999999999999984), np.float64(0.9999999999999998), np.float64(3.9999999999999982)]\n",
      "[np.float64(-1.0000000000000002), np.float64(-1.9999999999999984), np.float64(0.9999999999999998), np.float64(3.9999999999999982)]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "def guass(A,b):\n",
    "        n=len(A)\n",
    "        for k in range(n):\n",
    "                if A[k][k]==0:\n",
    "                        print(\"pivot==0\")\n",
    "                        return\n",
    "                else:\n",
    "                        for i in range(k+1,n):\n",
    "                                factor=A[i][k]/A[k][k]\n",
    "                                for j in range(n):\n",
    "                                        A[i][j]=A[i][j]-factor*A[k][j]\n",
    "                                b[i]=b[i]-factor*b[k]\n",
    "        x=[0 for i in range(n)]\n",
    "        for i in range(n-1,-1,-1):\n",
    "                x[i]=b[i]\n",
    "                for j in range(i+1,n):\n",
    "                        x[i]=x[i]-A[i][j]*x[j]\n",
    "\n",
    "                x[i]=x[i]/A[i][i]\n",
    "\n",
    "\n",
    "        return A,b,x\n",
    "\n",
    "def gs_iter(A,b,x0,tol=1e-6,max_iter=10000):\n",
    "        n=len(b)\n",
    "        x=x0\n",
    "        for k in range(max_iter):\n",
    "                for i in range(n):\n",
    "                        s=sum(A[i][j]*x[j] for j in range(n) if j!=i)\n",
    "\n",
    "                        x[i]=(b[i]-s)/A[i][i]\n",
    "                print(k,x)\n",
    "                A=np.array(A)\n",
    "                if all(abs(A.dot(x)-b)<tol):\n",
    "                        return x\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "        A0=[[2,2,3,1],[4,7,2,5],[2,4,5,2],[4,6,1,4]]\n",
    "        b0=[1,4,3,1]\n",
    "        A,B,x=guass(A0,b0)\n",
    "        #print(A,b,x)\n",
    "        x0=[0.,0.,0.,0.]\n",
    "        x=gs_iter(A0,b0,x0)\n",
    "        print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aa2d43b8-013f-40eb-a5cc-44a47246d014",
   "metadata": {},
   "outputs": [],
   "source": [
    "if __name__ == \"__main__\":\n",
    "        A0=[[2,2,3,1],[4,7,2,5],[2,4,5,2],[4,6,1,4]]\n",
    "        b0=[1,4,3,1]\n",
    "        A,B,x=guass(A0,b0)\n",
    "        #print(A,b,x)\n",
    "        x0=[0.,0.,0.,0.]\n",
    "        x=gs_iter(A0,b0,x0)\n",
    "        print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "71bbbd84-bb13-47db-abaf-dda87f520b4c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[2, 2, 3, 1], [0.0, 3.0, -4.0, 3.0], [0.0, 0.0, 4.666666666666666, -1.0], [0.0, 0.0, 0.0, -0.5000000000000001]] [1, 2.0, 0.6666666666666667, -1.9999999999999996] [-1.0000000000000004, -1.9999999999999984, 0.9999999999999998, 3.9999999999999982]\n",
      "[-1.0000000000000004, -1.9999999999999984, 0.9999999999999998, 3.9999999999999982]\n",
      "0 [0.5, 0.6666666666666666, 0.1428571428571429, 3.9999999999999982]\n",
      "1 [np.float64(-2.38095238095238), np.float64(-3.142857142857141), np.float64(0.9999999999999998), np.float64(3.9999999999999982)]\n",
      "2 [np.float64(0.14285714285714235), np.float64(-1.9999999999999984), np.float64(0.9999999999999998), np.float64(3.9999999999999982)]\n",
      "3 [np.float64(-1.0000000000000002), np.float64(-1.9999999999999984), np.float64(0.9999999999999998), np.float64(3.9999999999999982)]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "def gauss(A,b):\n",
    "        n=len(A)\n",
    "        for k in range(n):\n",
    "                if A[k][k]==0:\n",
    "                        print('pivot==0')\n",
    "                        return\n",
    "                else:\n",
    "                        for i in range(k+1,n):\n",
    "                                factor=A[i][k]/A[k][k]\n",
    "                                for j in range(n):\n",
    "                                        A[i][j]=A[i][j]-factor*A[k][j]\n",
    "                                b[i]=b[i]-factor*b[k]\n",
    "        x=[0 for i in range(n)]\n",
    "        for i in range(n-1,-1,-1):\n",
    "                x[i]=b[i]\n",
    "                for j in range(i+1,n):\n",
    "                        x[i]=x[i]-A[i][j]*x[j]\n",
    "                x[i]=x[i]/A[i][i]\n",
    "\n",
    "\n",
    "        return A,b,x\n",
    "\n",
    "def gs_iter(A,b,x0,tol=1e-6,max_iter=10000):\n",
    "        n=len(b)\n",
    "        x=x0\n",
    "        for k in range(max_iter):\n",
    "                for i in range(n):\n",
    "                        s=sum(A[i][j]*x[j] for j in range(n) if j!=i)\n",
    "                        x[i]=(b[i]-s)/A[i][i]\n",
    "                print(k,x)\n",
    "                A=np.array(A)\n",
    "                if all(abs(A.dot(x)-b)<tol):\n",
    "                        return x\n",
    "if __name__ == '__main__':\n",
    "        A0=[[2,2,3,1],[4,7,2,5],[2,4,5,2],[4,6,1,4]]\n",
    "        b0=[1,4,3,1]\n",
    "        A,b,x=gauss(A0,b0)\n",
    "        print(A,b,x)\n",
    "        #L,U=lu(A0)\n",
    "       # y=forward_substitution(L,b0)\n",
    "        #x=backward_substitution(U,y)\n",
    "        print(x)\n",
    "\n",
    "        x0=[0,0,0,0]\n",
    "        gs_iter(A0,b0,x0)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b0ff0915-b351-4c8d-8d50-7be6dbf7a0fe",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
